<div id="header" class="index container-fluid">
    <div class="con container">
        <div class="row">
            <div class="col-md-8 col-lg-8">
                <a href="/{{ site.subDomain }}/default" title="{{ site.title}}" class="logo"><img src="/images/default-logo.jpg"/></a>
                <div class="dropdown area">
                    <button class="btn btn-default dropdown-toggle btn-lg" type="button" id="dropdownMenu1" data-toggle="dropdown">
                        {{ site.name }}
                        <span class="caret"></span>
                    </button>

                    <!-- <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
                        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">杭州</a></li>
                        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">成都</a></li>
                        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">无锡</a></li>
                        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">北京</a></li>
                    </ul> -->
                    <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
                        {{each sites}}
                        <li role="presentation"><a role="menuitem" tabindex="-1" href="/{{ $value.subDomain }}/">{{ $value.name }}</a>
                        </li>
                        {{/each}}
                    </ul>
                </div>
                <style media="screen">
                .input-group .input-search-text {
                    padding-left: 60px;
                }
                .input-search-term {
                    position: absolute;
                    left: 0;
                    top: 0;
                    width: 55px;
                    height: 46px;
                    text-align: center;
                    line-height: 46px;
                    z-index: 3;
                    cursor: pointer;
                }
                .input-search-term:after {
                    position: absolute;
                    right: 0;
                    top: 21px;
                    content: '';
                    display: block;
                    width: 0px;
                    height: 0px;
                    border-top: 5px solid #333;
                    border-left: 5px solid rgba(0,0,0,0);
                    border-right: 5px solid transparent;
                    border-bottom: 5px solid transparent;
                }
                    .form-angle {
                        border-radius: 0;
                    }
                </style>
                <div class="input-group input-group-lg search">
                    <span class="input-search-term">转让</span>
                    <input type="text" class="form-control input-search-text">
                    <span class="input-group-btn">
                <button class="btn btn-default" type="button" id="search"><i class="fa fa-search"></i></button>
              </span>
                </div>
            </div>
            <div class="text-right col-md-4 col-lg-4">
                <a href="/{{ site.subDomain }}/publish" class="btn btn-default">免费发布信息</a>
                <a href="/{{ site.subDomain }}/signin" class="btn btn-theme-line no-user">登录</a>
                <a href="/{{ site.subDomain }}/register" class="btn btn-link no-user">注册</a>
                <a href="/{{ site.subDomain }}/percenter" class="btn btn-link has-user"></a>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    // 进入页面时，先判断登陆状态
    function checkLoginStatus () {
      var storeToken = localStorage.getItem('tengpu_jwt_token');

      if (storeToken) {
        $.ajax({
            type: 'get',
            url: '{{site_addr}}/loginuser?access_token='+ storeToken,
            success: function (data, textStatus) {
            //   console.log(data);

              // 已有登陆用户
              if (data && data.user && data.user.name) {
                userName = data.user.name;
                hasUser();
              }
              else {
                localStorage.removeItem('tengpu_jwt_token');
                location.reload();
              }
            }
        });
      }
    }

    function hasUser() {
        $('.has-user').text(userName).show();
        $('.no-user').hide();

        loginCb.forEach(function (item, idx) {
            // console.log(item);
            item && item();
        });
    }
    $('.dropdown-toggle').dropdown();
    // 已有登陆用户信息回调
    var loginCb = [];
    // 登陆用户信息

    var SITE_ADDR = '{{site_addr}}';
    var SITE_ID = '{{@ site._id }}'
    var userName = '{{userinfo ? userinfo.username : ""}}';
    var loginUser = {{@ userinfoStr}} || null;
    var CITY = '{{ site.subDomain }}' || 'nj';
    var CITY_ID = '{{@ site.city.oid }}'
    if (!userName) {
        checkLoginStatus();
    }
    else {
        // 让子弹飞一会
        setTimeout(function () {
            hasUser();
        }, 1);
    }
    // 注销用户 JWT方式只要删除localStorage里的token和cookie里的access_token
    function loginOut () {
        localStorage.removeItem('tengpu_jwt_token');

        $.ajax({
            type: 'get',
            url: '{{site_addr}}/logoutuser',
            success: function (data, textStatus) {
              if (data.ok) {
                location.href = '/'+CITY+'/signin';
              }
            }
        })
    }
    var search_infotype = 'Attorn';
    var $searchTerm = jQuery('.input-search-term');
    $searchTerm.on('click',function(){
        if (this.innerText == '转让') {
            this.innerText = '求租'
            search_infotype = 'SeekLease'
        } else {
            this.innerText = '转让'
            search_infotype = 'Attorn'
        }
    })
    function searchquery(keyword) {
        var query = {
              title: keyword,
              content: keyword,
              category: '', // 行业
              "estate.type":'',
              infoType: search_infotype, // 转让
              estateType: '', // 商铺类型
              "estate.district": '', // 行政区
              "estate.shopArea": '', // 商圈
              "estate.subway": '', // 地铁
              "estate.transferFee": '', // 转让费
              "estate.area": '', // 面积
              "estate.rental":'', // 租金
              // "estate.city": CITY // 租金
        }
        var sort = {
            createdAt: -1
        };

        var page = {
            index: 1,
            size: 10
        }
        setTimeout(function(){
            location.href = "/"+CITY+"/search?query="+JSON.stringify(query)+"&sort="+JSON.stringify(sort)+"&page="+JSON.stringify(page);
        },200);
    }
    jQuery('#search').on('click',function(){
        var data = jQuery('.search .form-control').val();
        console.log('搜索内容')
        console.log(data.split(' '))
        var q = data.split(' ');
        searchquery(q)
      //   location.href = '/'+CITY+'/search?query='+q;
    })
</script>
